package com.homeinns.lyx.trainserver.service.impl;

import com.homeinns.common.DecreaseStockInput;
import com.homeinns.common.ProductInfoOutput;
import com.homeinns.lyx.trainserver.dataobject.ProductInfo;
import com.homeinns.lyx.trainserver.enums.ResultEnum;
import com.homeinns.lyx.trainserver.excepion.ProductNotFoundException;
import com.homeinns.lyx.trainserver.repository.ProductInfoRepository;
import com.homeinns.lyx.trainserver.service.ProductService;
import com.homeinns.lyx.trainserver.utils.JsonUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<ProductInfo> findAllProduct(Integer status) {

        return productInfoRepository.findByProductStatus(status);
    }

    @Override
    public List<ProductInfoOutput> findList(List<String> productIds) {
        return productInfoRepository.findByProductIdIn(productIds).stream().map(x->{
            ProductInfoOutput output=new ProductInfoOutput();
            BeanUtils.copyProperties(x,output);
            return output;
        }).collect(Collectors.toList());
    }

    @Override
    public void decreaseStock(List<DecreaseStockInput> decreaseStockInputs) {

        List<ProductInfo> productInfos=decreaseStockProcess(decreaseStockInputs);
        List<ProductInfoOutput> productInfoOutputs = productInfos.stream().map(e -> {
            ProductInfoOutput output=new ProductInfoOutput();
            BeanUtils.copyProperties(e,output);
            return output;
        }).collect(Collectors.toList());
        amqpTemplate.convertAndSend("lyx-productInfo", JsonUtil.toJson(productInfoOutputs));

    }

    @Transactional
    public List<ProductInfo> decreaseStockProcess(List<DecreaseStockInput> decreaseStockInputs){

        List<ProductInfo> productInfos = new ArrayList<>();
        for (DecreaseStockInput decreaseStockInput:decreaseStockInputs){
            Optional<ProductInfo> productInfo = productInfoRepository.findById(decreaseStockInput.getProductId());
            if(!productInfo.isPresent()){
                throw new ProductNotFoundException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            ProductInfo prod = productInfo.get();
            Integer result = prod.getProductStock() - decreaseStockInput.getProductQuantity();
            if(result<0){
                throw new ProductNotFoundException(ResultEnum.PRODUCT_STOCK_ERROR);
            }

            prod.setProductStock(result);
            productInfoRepository.save(prod);
            productInfos.add(prod);

        }
        return productInfos;
    }
}
